# 获取股票上涨片段
from tools.dbTool import dbTool

codes = ['002591.SZ', '002205.SZ', '003017.SZ', '003003.SZ', '002295.SZ', '002809.SZ', '002830.SZ', '002836.SZ',
         '002828.SZ', '002144.SZ', '002800.SZ', '002853.SZ', '002817.SZ', '002209.SZ', '002858.SZ', '002760.SZ',
         '002278.SZ', '002909.SZ', '002998.SZ', '002871.SZ', '002357.SZ', '003036.SZ', '002826.SZ', '002968.SZ',
         '002802.SZ', '002942.SZ', '002873.SZ', '002687.SZ', '002715.SZ', '002566.SZ', '002576.SZ', '002811.SZ',
         '002365.SZ', '002420.SZ', '002729.SZ', '003018.SZ', '003033.SZ', '002753.SZ', '002394.SZ', '002774.SZ',
         '002732.SZ', '002172.SZ', '002012.SZ', '002622.SZ', '002825.SZ', '002671.SZ', '002442.SZ', '002066.SZ',
         '002931.SZ', '002879.SZ', '002159.SZ', '002806.SZ', '002381.SZ', '002674.SZ', '002949.SZ', '002982.SZ',
         '002863.SZ', '002286.SZ', '002860.SZ', '002772.SZ', '002476.SZ', '002903.SZ', '002730.SZ', '002107.SZ',
         '002864.SZ', '002363.SZ', '002098.SZ', '002927.SZ', '002899.SZ', '002660.SZ', '002451.SZ', '002112.SZ',
         '002734.SZ', '002769.SZ', '002796.SZ', '002164.SZ', '002398.SZ', '002578.SZ', '002692.SZ', '002412.SZ',
         '002969.SZ', '002021.SZ', '002337.SZ', '002443.SZ', '002661.SZ', '002790.SZ', '002788.SZ', '002956.SZ',
         '002852.SZ', '002900.SZ', '002917.SZ', '002391.SZ', '002981.SZ', '002548.SZ', '002040.SZ', '002598.SZ',
         '002616.SZ', '002688.SZ', '002535.SZ', '002404.SZ', '002560.SZ', '002686.SZ', '002722.SZ', '002918.SZ',
         '002165.SZ', '003020.SZ', '002885.SZ', '002546.SZ', '002350.SZ', '002090.SZ', '002565.SZ', '002980.SZ',
         '003025.SZ', '002666.SZ', '002862.SZ', '002348.SZ', '002201.SZ', '003037.SZ', '003010.SZ', '002228.SZ',
         '002615.SZ', '002835.SZ', '002333.SZ', '002471.SZ', '002735.SZ', '002282.SZ', '002628.SZ', '002627.SZ',
         '002919.SZ', '002630.SZ', '003015.SZ', '002296.SZ', '002186.SZ', '002448.SZ', '002580.SZ', '002462.SZ',
         '002631.SZ', '002082.SZ', '002427.SZ', '002973.SZ', '002364.SZ', '002290.SZ', '002953.SZ', '002224.SZ',
         '002361.SZ', '003028.SZ', '002783.SZ', '002160.SZ', '002132.SZ', '002395.SZ', '002300.SZ', '003019.SZ',
         '002457.SZ', '002479.SZ', '002606.SZ', '002599.SZ', '002658.SZ', '002474.SZ', '002952.SZ', '002346.SZ',
         '002454.SZ', '002441.SZ', '002870.SZ', '002358.SZ', '002996.SZ', '003002.SZ', '002645.SZ', '002161.SZ',
         '002393.SZ', '002067.SZ', '002655.SZ', '002886.SZ', '002849.SZ', '002150.SZ', '002355.SZ', '002577.SZ',
         '002644.SZ', '002137.SZ', '002971.SZ', '002757.SZ', '002728.SZ', '002328.SZ', '002134.SZ', '002876.SZ',
         '002986.SZ', '002380.SZ', '002614.SZ', '002187.SZ', '002613.SZ', '002824.SZ', '002758.SZ', '002083.SZ',
         '002983.SZ', '002094.SZ', '002478.SZ', '002562.SZ', '002345.SZ', '002452.SZ', '002912.SZ', '002589.SZ',
         '002033.SZ', '002255.SZ', '002843.SZ', '002810.SZ', '002962.SZ', '002823.SZ', '002676.SZ', '002116.SZ',
         '002026.SZ', '002483.SZ', '002406.SZ', '002584.SZ', '002549.SZ', '002322.SZ', '002272.SZ', '002117.SZ',
         '002062.SZ', '002264.SZ', '002552.SZ', '002178.SZ', '002677.SZ', '002637.SZ', '002029.SZ', '002937.SZ',
         '002766.SZ', '003005.SZ', '002733.SZ', '002376.SZ', '002135.SZ', '002206.SZ', '003001.SZ', '002845.SZ',
         '002869.SZ', '002866.SZ', '002218.SZ', '002553.SZ', '002392.SZ', '002181.SZ', '002449.SZ', '002896.SZ',
         '002632.SZ', '002570.SZ', '002104.SZ', '002639.SZ', '002375.SZ', '002250.SZ', '002746.SZ', '002930.SZ',
         '002752.SZ', '002988.SZ', '002950.SZ', '002367.SZ', '002321.SZ', '002119.SZ', '002538.SZ', '002491.SZ',
         '002838.SZ', '002469.SZ', '002695.SZ', '002303.SZ', '002609.SZ', '002801.SZ', '002059.SZ', '002907.SZ',
         '002437.SZ', '002803.SZ', '002641.SZ', '003000.SZ', '002662.SZ', '002445.SZ', '002009.SZ', '002636.SZ',
         '002768.SZ', '002401.SZ', '002284.SZ', '002782.SZ', '002111.SZ', '002301.SZ', '002166.SZ', '002175.SZ',
         '002955.SZ', '002057.SZ', '002559.SZ', '002344.SZ', '002088.SZ', '002334.SZ', '002190.SZ', '002881.SZ',
         '002965.SZ', '002017.SZ', '002014.SZ', '002884.SZ', '002749.SZ', '002765.SZ', '002922.SZ', '002481.SZ',
         '002283.SZ', '002990.SZ', '002889.SZ', '002154.SZ', '002470.SZ', '002122.SZ', '002108.SZ', '002039.SZ',
         '002536.SZ', '002438.SZ', '002034.SZ', '002880.SZ', '003013.SZ', '002446.SZ', '002035.SZ', '002191.SZ',
         '002957.SZ', '002959.SZ', '002961.SZ', '002935.SZ', '002349.SZ', '002293.SZ', '002258.SZ', '002530.SZ',
         '002997.SZ', '002991.SZ', '002208.SZ', '002649.SZ', '002707.SZ', '002167.SZ', '002519.SZ', '002102.SZ',
         '002882.SZ', '002103.SZ', '002590.SZ', '002651.SZ', '002533.SZ', '002540.SZ', '002125.SZ', '002140.SZ',
         '003012.SZ', '002274.SZ', '002046.SZ', '002543.SZ', '002390.SZ', '002913.SZ', '002042.SZ', '002106.SZ',
         '002440.SZ', '002327.SZ', '002567.SZ', '002612.SZ', '002706.SZ', '002970.SZ', '002149.SZ', '002238.SZ',
         '002985.SZ', '002685.SZ', '002726.SZ', '002897.SZ', '002421.SZ', '002827.SZ', '002170.SZ', '002941.SZ',
         '002023.SZ', '002833.SZ', '002550.SZ', '002727.SZ', '002929.SZ', '002123.SZ', '002777.SZ', '002093.SZ',
         '002276.SZ', '002212.SZ', '002697.SZ', '002960.SZ', '002840.SZ', '002792.SZ', '002254.SZ', '002073.SZ',
         '002275.SZ', '002947.SZ', '002534.SZ', '002516.SZ', '002101.SZ', '003039.SZ', '002174.SZ', '002588.SZ',
         '002434.SZ', '002041.SZ', '002233.SZ', '002581.SZ', '002378.SZ', '002100.SZ', '002079.SZ', '002302.SZ',
         '002194.SZ', '002993.SZ', '002004.SZ', '002859.SZ', '002791.SZ', '002542.SZ', '002267.SZ', '002987.SZ',
         '002979.SZ', '002192.SZ', '002043.SZ', '002539.SZ', '002891.SZ', '002626.SZ', '002545.SZ', '002967.SZ',
         '002081.SZ', '002045.SZ', '002396.SZ', '002818.SZ', '002892.SZ', '002928.SZ', '002467.SZ', '002703.SZ',
         '002635.SZ', '002807.SZ', '002204.SZ', '002158.SZ', '002239.SZ', '002716.SZ', '002048.SZ', '002338.SZ',
         '002335.SZ', '002488.SZ', '002675.SZ', '002216.SZ', '002243.SZ', '002839.SZ', '002832.SZ', '002036.SZ',
         '002020.SZ', '002051.SZ', '002518.SZ', '002317.SZ', '003006.SZ', '002815.SZ', '002946.SZ', '002925.SZ',
         '002182.SZ', '002061.SZ', '002011.SZ', '002978.SZ', '002324.SZ', '002498.SZ', '002332.SZ', '002705.SZ',
         '002287.SZ', '002668.SZ', '002458.SZ', '002237.SZ', '002605.SZ', '002895.SZ', '002737.SZ', '002063.SZ',
         '002091.SZ', '002245.SZ', '002643.SZ', '002315.SZ', '002541.SZ', '002690.SZ', '002745.SZ', '002617.SZ',
         '002755.SZ', '002654.SZ', '002292.SZ', '002901.SZ', '002015.SZ', '002597.SZ', '002183.SZ', '002249.SZ',
         '002701.SZ', '002497.SZ', '002351.SZ', '002847.SZ', '002019.SZ', '002867.SZ', '002003.SZ', '002487.SZ',
         '002226.SZ', '002060.SZ', '002126.SZ', '002320.SZ', '002468.SZ', '002911.SZ', '002400.SZ', '002484.SZ',
         '002268.SZ', '002408.SZ', '002270.SZ', '002139.SZ', '002557.SZ', '002428.SZ', '002611.SZ', '002221.SZ',
         '002096.SZ', '002368.SZ', '002506.SZ', '002906.SZ', '002531.SZ', '002222.SZ', '002507.SZ', '002399.SZ',
         '002747.SZ', '002958.SZ', '002572.SZ', '002266.SZ', '002698.SZ', '002773.SZ', '002416.SZ', '002145.SZ',
         '002402.SZ', '002044.SZ', '002563.SZ', '002299.SZ', '002203.SZ', '003021.SZ', '002936.SZ', '002385.SZ',
         '002461.SZ', '002372.SZ', '003022.SZ', '002607.SZ', '002432.SZ', '002683.SZ', '002410.SZ', '002080.SZ',
         '002508.SZ', '002948.SZ', '002155.SZ', '002851.SZ', '002056.SZ', '002831.SZ', '002120.SZ', '002926.SZ',
         '002429.SZ', '002318.SZ', '002756.SZ', '002430.SZ', '002500.SZ', '002130.SZ', '002558.SZ', '002472.SZ',
         '002138.SZ', '002025.SZ', '002261.SZ', '003031.SZ', '002984.SZ', '002841.SZ', '002065.SZ', '002568.SZ',
         '002262.SZ', '002837.SZ', '002739.SZ', '002850.SZ', '002738.SZ', '002008.SZ', '002603.SZ', '002244.SZ',
         '002517.SZ', '002608.SZ', '002273.SZ', '002821.SZ', '002966.SZ', '002409.SZ', '002157.SZ', '002007.SZ',
         '002583.SZ', '002444.SZ', '002152.SZ', '002131.SZ', '002281.SZ', '002271.SZ', '002414.SZ', '002223.SZ',
         '002939.SZ', '002812.SZ', '002595.SZ', '002340.SZ', '002423.SZ', '002555.SZ', '002312.SZ', '002353.SZ',
         '002797.SZ', '002180.SZ', '002532.SZ', '002294.SZ', '002185.SZ', '002945.SZ', '002673.SZ', '002085.SZ',
         '002074.SZ', '002078.SZ', '002064.SZ', '002653.SZ', '002601.SZ', '002032.SZ', '002709.SZ', '002384.SZ',
         '002456.SZ', '002128.SZ', '002156.SZ', '002202.SZ', '002252.SZ', '002916.SZ', '002236.SZ', '002459.SZ',
         '002422.SZ', '002049.SZ', '002028.SZ', '002648.SZ', '002600.SZ', '002001.SZ', '002920.SZ', '002463.SZ',
         '002311.SZ', '002938.SZ', '002460.SZ', '002179.SZ', '002625.SZ', '002241.SZ', '002050.SZ', '002493.SZ']
resDict = []
codePos = "002576.SZ"
for code in codes:
    if codePos == code:
        codePos = 0
    if codePos != 0:
        continue
    # codePos += 1
    # if codePos < 10:
    #     continue
    # if codePos > 10:
    #     break
    print(code)
    sql = f"select * from ts_daily_hfq where ts_code='{code}' and trade_date > '20230101' order by trade_date asc"
    df = dbTool.getPdList(sql)
    df['mv'] = df['close'].rolling(5).mean()
    # df['change'] = df['m5'].pct_change(5)
    df['change'] = df['close'].pct_change(5)
    dataLen = len(df)
    startDate = 0
    endDate = 0
    for i in range(dataLen):
        if i < 5:
            continue
        if df['change'][i] > 0.3:
            curDate = df.loc[i]['trade_date']
            if startDate <= curDate <= endDate:
                continue
            if i - 10 < 0:
                continue
            preDate = df.loc[i - 10]['trade_date']
            startDate = df.loc[i - 5]['trade_date']
            endDate = curDate
            j = i
            while True:
                j += 1
                if j < dataLen and df['mv'][j] > df['mv'][j - 1]:
                    endDate = df.loc[j]['trade_date']
                else:
                    resItem = {
                        'code': code,
                        'pre_date': preDate,
                        'start_date': startDate,
                        'end_date': endDate
                    }
                    print(f"片段: {preDate},{startDate},{endDate}")
                    resDict.append(resItem)
                    break
    dbTool.saveAll("rs_up_snippet", resDict, tableComment="向上片段")
    # break
